<template>
  <div class="login">
    <el-row>
      <el-col :span="12" class="login-left">
        <img :src="logoImg" class="logo-img" />
      </el-col>
      <el-col :span="12" class="login-right">
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
          <h3 class="title">登录账号</h3>
          <el-tabs v-model="loginType">
            <el-tab-pane label="账号密码登录" name="password">
              <el-form-item prop="username">
                <el-input
                  v-model="loginForm.username"
                  type="text"
                  auto-complete="off"
                  placeholder="账号"
                >
                  <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
                </el-input>
              </el-form-item>
              <el-form-item prop="password">
                <el-input
                  v-model="loginForm.password"
                  type="password"
                  auto-complete="off"
                  placeholder="密码"
                  @keyup.enter.native="handleLogin"
                >
                  <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
                </el-input>
              </el-form-item>
              <el-form-item prop="code" v-if="captchaEnabled">
                <el-input
                  v-model="loginForm.code"
                  auto-complete="off"
                  placeholder="验证码"
                  style="width: 63%"
                  @keyup.enter.native="handleLogin"
                >
                  <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
                </el-input>
                <div class="login-code">
                  <img :src="codeUrl" @click="getCode" class="login-code-img"/>
                </div>
              </el-form-item>
              <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
            </el-tab-pane>
            <!-- <el-tab-pane label="手机快捷登录" name="sms">
              <el-form-item prop="phone">
                <el-input
                  v-model="loginForm.phone"
                  type="text"
                  auto-complete="off"
                  placeholder="手机号"
                >
                  <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
                </el-input>
              </el-form-item>
              <el-form-item prop="code">
                <el-input placeholder="请输入验证码" v-model="loginForm.code">
                  <template slot="append">
                    <el-button type="text" class="get-msg-btn" @click="getSms">
                      {{ getMessage ? time+'s' : '获取验证码' }}
                    </el-button>
                  </template>
                </el-input>
              </el-form-item>
            </el-tab-pane> -->
          </el-tabs>

          <el-form-item style="width:100%;">
            <el-button
              :loading="loading"
              class="login-btn"
              size="medium"
              type="primary"
              style="width:100%;"
              @click.native.prevent="handleLogin"
            >
              <span v-if="!loading">立 即 登 录</span>
              <span v-else>登 录 中...</span>
            </el-button>
            <div style="float: right;" v-if="register">
              <router-link class="link-type" :to="'/register'">立即注册</router-link>
            </div>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 复向丝泰医疗科技（苏州）有限公司.</span>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'
import logoImg from '@/assets/images/logo.png'

// 获取短信事件间隔
const DEFAULT_TIME = 60;

export default {
  name: "Login",
  data() {
    return {
      logoImg,
      codeUrl: "",
      loginType: 'password',
      time: DEFAULT_TIME,
      getMessage: false,
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
      },
      loading: false,
      // 验证码开关
      captchaEnabled: false,
      // 注册开关
      register: false,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getCookie();
  },
  methods: {
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : password,
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", this.loginForm.password, { expires: 30 });
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove('rememberMe');
          }
          let params = {
            ...this.loginForm,
            password: encrypt(this.loginForm.password)
          }
          this.$store.dispatch("Login", params).then(() => {
            this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
          }).catch(() => {
            this.loading = false;
          });
        }
      });
    },
    // 获取验证码
    getSms() {
      if (this.getMessage) {
        return;
      }
      this.runTime();
    },
    // 倒计时
    runTime() {
      if (this.time > 0 ) {
        this.getMessage = true;
        setTimeout(() => {
          this.time--;
          this.runTime();
        }, 1000);
      } else {
        this.time = DEFAULT_TIME;
        this.getMessage = false;
      }
    }
  }
};
</script>

<style lang="scss" scope>
.login {
  // display: flex;
  // justify-content: center;
  // align-items: center;
  height: 100%;
  // background-size: cover;
  .el-row, .el-col{
    height: 100%;
  }
}

.login-left {
  position: relative;
  background-image: url(../assets/images/login-bg.png);
  background-size: 70%;
  background-position: center bottom 40%;
  background-repeat: no-repeat;
}
.logo-img {
  margin: auto;
  display: block;
  margin-top: 50px;
}

.login-right{
  position: relative;
  background-color: rgb(227, 93, 87);
}
.title {
  letter-spacing: 4px;
  font-size: 28px;
  margin: 0px auto 30px auto;
  text-align: center;
  color: #333333;
}

.login-form {
  position: relative;
  margin: auto;
  border-radius: 12px;
  background: #ffffff;
  width: 400px;
  padding: 50px 35px 25px 35px;
  height: 440px;
  top: 50%;
  margin-top: -220px;

  .el-tabs__item {
    font-size: 16px;
    color: #b7b7b7;
  }
  .el-tabs__item.is-active{
    color: #333333;
  }
  .el-tabs__nav-wrap::after, .el-tabs__active-bar{
    display: none;
  }
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.login-btn {
  background-color: rgb(227, 93, 87);
  border-color: rgb(227, 93, 87);
  padding:20px 20px;
  font-size: 18px;
  border-radius: 0 10px 10px 0;
  background-image: url(../assets/images/login-arrow.png) !important;
  background-repeat: no-repeat !important;
  background-position: right 40px center !important;
}
.login-btn:hover{
  background-color: #d55c57; 
  border-color: #d55c57; 
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #333333;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-code-img {
  height: 38px;
}
</style>
<style lang="scss" scoped>
.get-msg-btn {
  padding:0 10px;
  width: 100px;
}
</style>